#include "animatedrectitem.h"
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QInputDialog>

AnimatedRectItem::AnimatedRectItem(const QRectF &rect, int value, Node* nodePointer, QGraphicsItem *parent)
    : QGraphicsObject(parent), m_rect(rect), data(value), nodePointer(nodePointer) {}

QRectF AnimatedRectItem::geometry() const {
    return m_rect;
}

void AnimatedRectItem::setGeometry(const QRectF &rect) {
    if (m_rect != rect) {
        prepareGeometryChange();
        m_rect = rect;
        update();
    }
}

QRectF AnimatedRectItem::boundingRect() const {
    return m_rect;
}

void AnimatedRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setPen(QPen(Qt::black, 2)); // 边框粗细设置为2
    painter->setBrush(Qt::yellow);
    painter->drawRoundedRect(m_rect, 10, 10); // 绘制圆角矩形

    // 设置字体大小
    QFont font = painter->font();
    font.setPointSize(18); // 调大字体大小
    painter->setFont(font);

    // 绘制文本
    painter->drawText(m_rect, Qt::AlignCenter, QString::number(data));
}

// 添加鼠标点击事件处理器
void AnimatedRectItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    bool ok;
    int newValue = QInputDialog::getInt(nullptr, "修改值", "输入新的数值:", data, -2147483647, 2147483647, 1, &ok);
    if (ok) {
        data = newValue; // 更新数据值
        emit valueChanged(newValue, nodePointer); // 触发信号，通知主窗口修改值
        update(); // 重新绘制
    }
    QGraphicsItem::mousePressEvent(event);
}
